package org.jeecg.modules.demo.mes.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.mes.entity.CamelliaMesOrder;
import org.jeecg.modules.demo.mes.entity.CamelliaMesProcessPlan;
import org.jeecg.modules.demo.mes.entity.CamelliaMesProduct;
import org.jeecg.modules.demo.mes.entity.OrderDot;
import org.jeecg.modules.demo.mes.mapper.CamelliaMesOrderMapper;
import org.jeecg.modules.demo.mes.service.ICamelliaMesOrderService;
import org.jeecg.modules.demo.mes.service.ICamelliaMesProcessPlanService;
import org.jeecg.modules.demo.mes.service.ICamelliaMesProductService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 订单表
 * @Author: jeecg-boot
 * @Date:   2022-12-01
 * @Version: V1.0
 */
@Api(tags="订单表")
@RestController
@RequestMapping("/mes/camelliaMesOrder")
@Slf4j
public class CamelliaMesOrderController extends JeecgController<CamelliaMesOrder, ICamelliaMesOrderService> {

	@Resource
	private CamelliaMesOrderMapper camelliaMesOrderMapper;
	@Autowired
	private ICamelliaMesOrderService camelliaMesOrderService;

	@Autowired
	private ICamelliaMesProductService productService;

	@Autowired
	private ICamelliaMesProcessPlanService planService;

	/**
	 * 分页列表查询
	 *
	 * @param camelliaMesOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "订单表-分页列表查询")
	@ApiOperation(value="订单表-分页列表查询", notes="订单表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CamelliaMesOrder>> queryPageList(CamelliaMesOrder camelliaMesOrder,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 HttpServletRequest req) {
		QueryWrapper<CamelliaMesOrder> queryWrapper = QueryGenerator.initQueryWrapper(camelliaMesOrder, req.getParameterMap());
		Page<CamelliaMesOrder> page = new Page<CamelliaMesOrder>(pageNo, pageSize);
		IPage<CamelliaMesOrder> pageList = camelliaMesOrderService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param camelliaMesOrder
	 * @return
	 */
	@AutoLog(value = "订单表-添加")
	@ApiOperation(value="订单表-添加", notes="订单表-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:camellia_mes_order:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CamelliaMesOrder camelliaMesOrder) {
		camelliaMesOrder.setOrderStatus(0);
		CamelliaMesOrder order = camelliaMesOrder;
		CamelliaMesProcessPlan camelliaMesProcessPlan = new CamelliaMesProcessPlan();
		camelliaMesProcessPlan.setProcessStatus(-3);
		camelliaMesProcessPlan.setOrderNum(order.getOrderNum());
//		camelliaMesProcessPlan.setMaterialIds(order.get)
		camelliaMesOrderService.save(camelliaMesOrder);
		camelliaMesProcessPlan.setOrderId(camelliaMesOrder.getId());

		CamelliaMesProduct product = productService.getById(order.getRequiredProduct());
		camelliaMesProcessPlan.setOrderProduct(product.getName());

		//查找工艺
		String requiredProduct = order.getRequiredProduct();
		String requiredCraft = product.getRequiredCraft();
		String[] split = requiredCraft.split(",");
		String craftid;
		if(requiredCraft.length()>0) {
			craftid = split[0];
			camelliaMesProcessPlan.setCrafts(craftid);
		}
		else{
//			return Result.error("缺少工艺");
			;
		}

		//生产订单编号
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSSSSS");
		Date date = new Date();
		String code = dateFormat.format(date);
		camelliaMesProcessPlan.setCode(code);

		planService.save(camelliaMesProcessPlan);
		camelliaMesOrder.setProcessPlanId(camelliaMesProcessPlan.getId());
		camelliaMesOrderService.updateById(camelliaMesOrder);

//		QueryWrapper<CamelliaMesProcessPlan> planQueryWrapper = new QueryWrapper<>();
//		planQueryWrapper.eq("order_id",order.getId());
//		planService.list();


//		System.out.println(camelliaMesOrder.getId());
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param camelliaMesOrder
	 * @return
	 */
	@AutoLog(value = "订单表-编辑")
	@ApiOperation(value="订单表-编辑", notes="订单表-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:camellia_mes_order:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CamelliaMesOrder camelliaMesOrder) {
		camelliaMesOrderService.updateById(camelliaMesOrder);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "订单表-通过id删除")
	@ApiOperation(value="订单表-通过id删除", notes="订单表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:camellia_mes_order:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		camelliaMesOrderService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "订单表-批量删除")
	@ApiOperation(value="订单表-批量删除", notes="订单表-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:camellia_mes_order:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.camelliaMesOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "订单表-通过id查询")
	@ApiOperation(value="订单表-通过id查询", notes="订单表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CamelliaMesOrder> queryById(@RequestParam(name="id",required=true) String id) {
		CamelliaMesOrder camelliaMesOrder = camelliaMesOrderService.getById(id);
		if(camelliaMesOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(camelliaMesOrder);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param camelliaMesOrder
	 */
	//@RequiresPermissions("org.jeecg.modules.demo:camellia_mes_order:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, CamelliaMesOrder camelliaMesOrder) {
		return super.exportXls(request, camelliaMesOrder, CamelliaMesOrder.class, "订单表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	//@RequiresPermissions("camellia_mes_order:importExcel")
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, CamelliaMesOrder.class);
	}

//	 /**
//	  * 需求来源
//	  */
//	 @RequestMapping(value = "/clientSource", method = RequestMethod.GET)
//	 public Result<?> clientSource(){
//		 List<OrderDot> orderDots = camelliaMesOrderMapper.clientSource();
//		 if (orderDots == null) {
//			 return Result.error("未找到对应数据");
//		 }
//		 return Result.OK(orderDots);
//	 }

	@RequestMapping(value = "/clientSource", method = RequestMethod.GET)
	public Result<?> clientSource(){
		List<OrderDot> orderDots = new ArrayList<>();
		int[] array = new int[5];
		List<CamelliaMesOrder> camelliaMesOrders = camelliaMesOrderMapper.selectList(null);
		for (CamelliaMesOrder camelliaMesOrder : camelliaMesOrders) {
			Integer orderStatus = camelliaMesOrder.getOrderStatus();
			if(orderStatus==-2){
				orderStatus=4;
			}
			array[orderStatus]+=camelliaMesOrder.getOrderNum();
		}
		orderDots.add(new OrderDot("待排单", (double) array[0]));
		orderDots.add(new OrderDot("待生产", (double) array[1]));
		orderDots.add(new OrderDot("生产中", (double) array[2]));
		orderDots.add(new OrderDot("生产完成", (double) array[3]));
		orderDots.add(new OrderDot("退回", (double) array[4]));
		if (orderDots == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(orderDots);
	}


	/**
	 * 需求占比
	 */
	@RequestMapping(value = "/salesProduct", method = RequestMethod.GET)
	public Result<?> salesProduct(){
		List<OrderDot> orderDots = camelliaMesOrderMapper.salesProduct();
		if (orderDots == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(orderDots);
	}

}



/**
 * 编号 生成器
 * @author Java自学通
 *
 */
final class MyIncrementGenerator {
	private MyIncrementGenerator() {
	}
	/**
	 * 得到32位唯一的UUID
	 *
	 * @return 唯一编号
	 */
	public static Serializable uuid() {
		UUID uid = UUID.randomUUID();
		return uid.toString().replace("-", "");
	}
	/**
	 * 根据长度得到唯一编号
	 *
	 * @param length
	 * 长度
	 * @return 唯一编号
	 */
	public static Serializable uuid(int length) {
		UUID uid = UUID.randomUUID();
		String temp = uid.toString().replace("-", "");
		if (length > 0 && length < temp.length()) {
			temp = temp.substring(temp.length() - length);
		}
		return temp;
	}
	/**
	 * 根据长度得到随机字符串，一位字母一位数字
	 *
	 * @param length
	 * 长度
	 * @return 字符串
	 */
	public static String unique(int length) {
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < length; i++) {
			if (i % 2 == 0) {
				str.append(getRandom(97, 122));
			} else {
				str.append(getRandom(48, 57));
			}
		}
		return str.toString();
	}
	/**
	 * 得到纯数字编号
	 *
	 * @param length
	 * 长度
	 * @return
	 */
	public static String number(int length) {
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < length; i++) {
			if (i == 0)
				str.append(getRandom(49, 57));
			else
				str.append(getRandom(48, 57));
		}
		return str.toString();
	}
	/**
	 * 根据开始和结束大小得到单一字符
	 *
	 * @param begin
	 * 开始值
	 * @param end
	 * 结束值
	 * @return 单一字符
	 */
	private static String getRandom(int begin, int end) {
		String str = "";
		Random rd = new Random();
		int number = 0;
		while (str.length() == 0) {
			number = rd.nextInt(end + 1);
			if (number >= begin && number <= end)
				str = String.valueOf((char) number);
		}
		return str;
	}
}
